package com.luanye.enjoy.utils;

import org.springframework.beans.BeanUtils;
import java.lang.reflect.Constructor;
import java.util.List;
import java.util.stream.Collectors;

public class BeanCopyUtils {

    private BeanCopyUtils(){}

    public static <V>V copyBean(Object source,Class<V> clazz){
        V result = null;
        try{
            Constructor<V> constructor = clazz.getDeclaredConstructor();
            constructor.setAccessible(true);
            result = constructor.newInstance();
            BeanUtils.copyProperties(source,result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static <V,O>List<V> copyBeanList(List<O> source,Class<V> clazz){
        return source.stream()
                .map(o -> copyBean(o, clazz))
                .collect(Collectors.toList());
    }
}
